<template>
  <a-modal :title="title" :width="800" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen
    @ok="handleOk" @cancel="handleCancel" cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">


        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="炼厂名称">
          <a-input placeholder="请输入炼厂名称" :disabled='true' v-model="refineryName" />
        </a-form-item>

        <a-form-item v-for="item in selectListName" :labelCol="labelCol" :wrapperCol="wrapperCol" :label="item.name">
          <div class="flex-b">
            <div v-for="_item in item.arr" style="display:flex;flex :1;">
              <div style="display: flex;" v-if="_item.type==2">
                <a-input :placeholder="'请输入'+item.name+'挂牌价'" v-model="_item.price" />
                <div style="display: flex;justify-content: center;">
                  <a-button type="primary" @click="toHisDetail(item,2)" style="margin-left: 10px;">
                    历史报价
                  </a-button>
                </div>
              </div>
              <div style="display: flex;" v-else>
                <a-input style="margin-left: 10px;" :placeholder="'请输入'+item.name+'优惠价'" v-model="_item.price" />
                <div style="display: flex;justify-content: center;">
                  <a-button type="primary" @click="toHisDetail(item,1)" style="margin-left: 10px;">
                    历史报价
                  </a-button>
                </div>
              </div>
            </div>
          </div>
        </a-form-item>

      </a-form>
    </a-spin>

    <YsyRefineryPriceHistModal ref="modalForm" @ok="modalFormOk"></YsyRefineryPriceHistModal>
  </a-modal>
</template>

<script>
  import {
    debounce
  } from 'lodash';
  import {
    httpAction,
    postAction,
    getAction
  } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JEditor from '@/components/jeecg/JEditor'
  import YsyRefineryPriceHistModal from './his/YsyRefineryPriceHistModal.vue'

  export default {
    components: {
      JEditor,
      YsyRefineryPriceHistModal
    },
    props: {
      type: {
        type: Number,
        default: 1
      }
    },
    name: "YsyRefineryModal",
    data() {
      return {
        selectList: [],
        value2: [],
        title: "分配油品",
        visible: false,
        model: {},
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          getOilTypeByRefineryId: '/app/ysyOilType/getOilTypeByRefineryId/',
          bjPrice: '/app/ysyRefineryPrice/bjPrice'
        },
        refineryId: '',
        list: [],
        oilRawMaterialIds: [],
        indexs: [],
        status: '',
        discountFlag: '1',
        refineryItem: '',
        refineryName: '',
        selectList: [],
        selectListName: [],
      }
    },
    created() {},
    methods: {
      modalFormOk() {

      },
      toHisDetail(e, type) {
        this.$refs.modalForm.edit(e.id, this.ysyRefineryId, type, e.name)
      },
      handleChange(e) {
        this.indexs = e
      },
      add() {
        this.edit({});
      },
      edit(record) {
        this.ysyRefineryId = record.refineryId
        this.getOilTypeByRefineryId()
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.refineryItem = record
        this.refineryName = record.refineryName

      },
      close() {
        this.$emit('close');
        this.visible = false;
        this.refineryId = ''
        this.value2 = []
        this.selectList = []
        this.selectListName = []
        this.clickFlag = true
      },
      getOilTypeByRefineryId() {
        getAction(this.url.getOilTypeByRefineryId + this.ysyRefineryId).then((res) => {
          this.selectList = res.result.map(item => item.id)
          res.result.forEach(item => {
            let arr = []
            let aobj = {
              type: 1,
              price: ''
            }
            let aobj1 = {
              type: 2,
              price: ''
            }
            arr.push(aobj1)
            arr.push(aobj)
            let obj = {
              name: item.name,
              id: item.id,
              arr: arr
            }
            this.selectListName.push(obj)
          })
          this.visible = true;
        })
      },
      handleOk() {
        this.selectListName.forEach(item => {
          this.deboseFuc(item)
        })
        this.selectListName = []
        this.visible = false;
      },
      // deboseFuc: debounce(function(item) {
      //   const that = this;
      //   let params = {
      //     ysyRefineryId: this.ysyRefineryId,
      //     selectNameList: item
      //   }
      //   postAction(this.url.bjPrice, params).then((res) => {
      //     this.$message.success('报价成功');
      //   })
      // }, 1000),
      deboseFuc(item) {
        const that = this;
        let params = {
          ysyRefineryId: this.ysyRefineryId,
          selectNameList: item
        }
        postAction(this.url.bjPrice, params).then((res) => {
          this.$message.success('报价成功');
        })
      },
      handleCancel() {
        this.close()
      },

    }
  }
</script>

<style lang="less" scoped>
  .flex-b {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
</style>